/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/


#include "MkTcp/MkTcpServer.h"
#include "MkEvent/MkEvent.h"
#include "MkEvent/MkEventPool.h"
#include "MkTcp/MkTcpSession.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkLog.h"
#include "MkUtil/MkThread.h"
#include <signal.h>

class CTestTcpSession:public CMkTcpSession
{
public:
    CTestTcpSession(CMkEvent* pEvent, CMkTimer* pTimer, CMkTcpServer* pServer);
    ~CTestTcpSession();

    //Uint32 OnRecved(Int8* pBuf, Uint32 nLen, OUT Uint32& Used) override;
protected:
private:
};

CTestTcpSession::CTestTcpSession(CMkEvent* pEvent, CMkTimer* pTimer, CMkTcpServer* pServer)
    :CMkTcpSession(pEvent,pTimer,pServer)
{

}

CTestTcpSession::~CTestTcpSession()
{

}

//Uint32 CTestTcpSession::OnRecved(Int8* pBuf, Uint32 nLen, OUT Uint32& Used)
//{
//    MkDebugLog("recv len:%d\n", nLen);
//    //return Send(pBuf, nLen);
//    return NoneError;
//}


class CTestTcpServer:public CMkTcpServer
{
public:
    CTestTcpServer(CMkEventPool* pEventPool, CMkTimer* pTimer, Uint32 MaxConnections);
    ~CTestTcpServer();

    CMkTcpSession* CreateNewSession(CMkEvent *pEvent, CMkTimer* pTimer) override;
protected:
private:
};



CTestTcpServer::CTestTcpServer(CMkEventPool* pEventPool, CMkTimer* pTimer, Uint32 MaxConnections)
    :CMkTcpServer(pEventPool, pTimer, MaxConnections)
{

}

CMkTcpSession* CTestTcpServer::CreateNewSession(CMkEvent *pEvent, CMkTimer* pTimer)
{
    return new CTestTcpSession(pEvent, pTimer, this);
}

CTestTcpServer::~CTestTcpServer()
{

}


CMkTimer     g_Timer;
int main(int argc, char ** argv)
{
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
#endif
    MkLogChangeLevel(LOG_DEBUG);
    CMkEventPool Pool;

    Pool.Start();
    CTestTcpServer TcpSever(&Pool, &g_Timer, 10000);
    TcpSever.StartServer(8888);

    signal(SIGINT, [](int sig) {
        if (SIGINT == sig) {
            g_Timer.LoopBreak();
        }
    });// set exit  signal

    g_Timer.Dispatch();
    Pool.Stop();
    TcpSever.StopServer();
#ifdef _WIN32
    WSACleanup();
#endif
printf("end\n");
    return 0;
}


